package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.BpmnInfo;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.mapper.BpmnInfoMapper;
import cn.wolfcode.car.business.qo.BpmnInfoQuery;
import cn.wolfcode.car.business.service.IBpmnInfoService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.config.SystemConfig;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import com.github.pagehelper.PageHelper;
import org.activiti.bpmn.model.BpmnModel;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.image.impl.DefaultProcessDiagramGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class BpmnInfoServiceImpl implements IBpmnInfoService {

    @Autowired
    private BpmnInfoMapper bpmnInfoMapper;
    @Autowired
    private RepositoryService repositoryService;


    @Override
    public TablePageInfo<BpmnInfo> query(BpmnInfoQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<BpmnInfo>(bpmnInfoMapper.selectForList(qo));
    }


    @Override
    public void save(BpmnInfo bpmnInfo) {
        bpmnInfoMapper.insert(bpmnInfo);
    }

    @Override
    public BpmnInfo get(Long id) {
        return bpmnInfoMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(BpmnInfo bpmnInfo) {
        bpmnInfoMapper.updateByPrimaryKey(bpmnInfo);
    }



    // 删除
    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            bpmnInfoMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public void delete(Long id) {
        // 获取bpmn对象
        BpmnInfo bpmnInfo = get(id);
        // 删除bpmn对象
        bpmnInfoMapper.deleteByPrimaryKey(id);
        // 删除流程定义
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .deploymentId(bpmnInfo.getDeploymentId())
                .singleResult();
        // 删除服务器上的文件
        // 获取文件名
        String resourceName = processDefinition.getResourceName();
        File file = new File(SystemConfig.getProfile(), resourceName);
        System.out.println(file);
        if (file.exists()){
            file.delete();
        }
        repositoryService.deleteDeployment(bpmnInfo.getDeploymentId(),true);
    }

    // 查看文件
    @Override
    public InputStream deploymentId(String deploymentId, String type) {
        // 获取流程定义对象
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .deploymentId(deploymentId)
                .singleResult();
        if ("xml".equals(type)){
            // xml 文件
            // 获取文件名
            String resourceName = processDefinition.getResourceName();
            InputStream inputStream = repositoryService.getResourceAsStream(deploymentId, resourceName);
            return inputStream;
        }else if ("png".equals(type)){
            // 处理pnj文件
            BpmnModel model = repositoryService.getBpmnModel(processDefinition.getId());
            DefaultProcessDiagramGenerator generator = new DefaultProcessDiagramGenerator();
            // generateDiagram(bpmn模型对象,需要高亮的节点,需要高亮的连线)
            InputStream inputStream = generator.generateDiagram
                    (model, Collections.EMPTY_LIST, Collections.EMPTY_LIST,"宋体", "宋体", "宋体");
            return inputStream;
        }
        throw new BusinessException("非法错误");
    }

    // 文件上传
    @Override
    public void deploy(String path, String bpmnType, String info) throws FileNotFoundException {
        // 获取文件  文件路径+文件名
        File file = new File(SystemConfig.getProfile(), path);
        // 将流程部署到Activiti中
        Deployment deploy = repositoryService.createDeployment()
                .addInputStream(path, new FileInputStream(file))
                .deploy();
        // 根据生成的部署对象 封装到bpmnInfo中
        BpmnInfo bpmnInfo = new BpmnInfo();
        bpmnInfo.setBpmnType(bpmnType);
        bpmnInfo.setInfo(info);
        bpmnInfo.setDeployTime(new Date());
        bpmnInfo.setDeploymentId(deploy.getId());
        // 获取流程定义对象
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .deploymentId(deploy.getId())
                .singleResult();
        bpmnInfo.setBpmnName(processDefinition.getName());
        bpmnInfo.setActProcessKey(processDefinition.getKey());
        bpmnInfo.setActProcessId(processDefinition.getId());
        // 添加 bpmnInfo
        bpmnInfoMapper.insert(bpmnInfo);
    }

    @Override
    public List<BpmnInfo> list() {
        return bpmnInfoMapper.selectAll();
    }
}

